package allwe.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionTest {

    private static ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        Condition condition = reentrantLock.newCondition();

        new Thread(() -> {
            reentrantLock.lock();
            System.out.println("子线程持有锁");

            try {
                Thread.sleep(3000);

                System.out.println("子线程挂起");
                condition.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            System.out.println("子线程持有锁");

        }).start();

        Thread.sleep(100);

        reentrantLock.lock();
        System.out.println("主线程获取锁资源");

        Thread.sleep(3000);
        condition.signal();
        System.out.println("主线程唤醒子线程");

        Thread.sleep(3000);
        System.out.println("主线程释放锁");
        reentrantLock.unlock();

    }

}
